//
//  FZOauthController.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/3/28.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit
import SVProgressHUD
class FZOauthController: UIViewController,UIWebViewDelegate {
        
   private lazy var web = UIWebView()
    
    override func loadView() {
        
        view = web
    }
    override func viewDidLoad() {
        super.viewDidLoad()
       
        setNavUI()
     
        loadWebView()
    }
    
   private func loadWebView() {
        
        let request = URLRequest(url: URL(string: "https://api.weibo.com/oauth2/authorize?client_id=3750074423&redirect_uri=http://www.itcast.cn")!)
        
        web.loadRequest(request)
        web.delegate = self
    }
    
   private func setNavUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, selector: #selector(dismisVc))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, selector: #selector(autoFill))
    }
    
   @objc private func dismisVc() {
        
        dismiss(animated: true, completion: nil)
    }
    
   @objc private func autoFill() {
        
        web.stringByEvaluatingJavaScript(from: "document.getElementById('userId').value = '15652390318';document.getElementById('passwd').value = '9049321929';")
    }
}
 //MARK:获取code值
extension FZOauthController {
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
       //http://www.itcast.cn/?code=0b1e695d55090a4e5cbcd31bdd599088
        
        guard let urlStr = request.url?.absoluteString else {
            
            return false
        }
        
        if !urlStr.hasPrefix(redirect_uri) {
            
           return true
        }
        
        //代码执行到此都是http://www.itcast.cn开头的
        if let queray = request.url?.query{
            
            if queray.hasPrefix("code=") {
                
                let code = queray.substring(from: "code=".endIndex)
                
                FZUserViewModel.sharedUser.loadUserData(code: code, successClosure: { (isSuccess) in
                    
                    if isSuccess {
                        print("登陆成功")
                        self.dismiss(animated: true, completion: {
                            
                          NotificationCenter.default.post(name: NSNotification.Name(rawValue: switchVc), object: self)
                        })
                        
                    }else{
                        
                        print("登陆失败")
                    }
                })
                
                
                
                                
            }else{
                
                dismiss(animated: true, completion: nil)//取消授权
            }
        }
        return false
    }
    
    
    
    
}

//webView代理方法
extension FZOauthController {
    
    func webViewDidStartLoad(_ webView: UIWebView) {
        SVProgressHUD.show()
        
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        
        SVProgressHUD.dismiss()
        
    }
    
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
    
        SVProgressHUD.dismiss()
    }
    
}











